package com.yuwenxin.dao.impl;

import com.yuwenxin.bean.Combination;
import com.yuwenxin.core.BaseDaoImpl;
import com.yuwenxin.core.BaseQueryModel;
import com.yuwenxin.dao.CombinationDao;

import java.sql.ResultSet;
import java.util.Date;
import java.util.HashSet;

public class CombinationDaoImpl extends BaseDaoImpl implements CombinationDao {
    @Override
    public String getSelectAllSql() {
        return "select * from combination";
    }

    @Override
    public String getSelectByNameSql() {
        return "select * from combination where singerName=?";
    }

    @Override
    public String getFuzzyFindSql() {
        return "select * from combination where singerName like ?";
    }

    @Override
    public String getUpdateSql(Object object) {
        return null;
    }

    @Override
    public String getDeleteSql() {
        return "delete from combination where idsinger=?";
    }

    @Override
    public String getSelectByIdSql() {
        return "select * from combination where idsinger=?";
    }

    @Override
    public String getInsertSql(Object object) {
        Combination _combination = (Combination) object;
        String singerName = _combination.getSingerName();
        String corporationName = _combination.getCorporationName();
        String singerPhotoPath = CombinationDao.DEFAULT_PATH;
        Date establishDate = _combination.getEstablishDate();


        if (corporationName == null){
            StringBuilder sb = new StringBuilder("insert into combination (singerName,singerPhotoPath,establishDate) values('");
            sb.append(singerName).append("','").append(singerPhotoPath).append("','").append(establishDate).append("')");
            return sb.toString();
        }
        StringBuilder sb = new StringBuilder("insert into combination (singerName,corporationName,singerPhotoPath,establishDate) values('");
        sb.append(singerName).append("','").append(corporationName).append("','").append(singerPhotoPath).append("','")
                .append(establishDate).append("')");
        return sb.toString();
    }

    @Override
    public String getConditionSql(BaseQueryModel queryModel) {
        return null;
    }

    @Override
    public Object toEntity(ResultSet rs) throws Exception {
        Combination combination = new Combination();
        combination.setId(rs.getInt("idcombination"));
        combination.setSingerName(rs.getString("singerName"));
        combination.setCorporationName(rs.getString("corporationName"));
        combination.setSingerPhotoPath(rs.getString("singerPhotoPath"));
        combination.setEstablishDate(rs.getDate("establishDate"));
        combination.setMusicSet(new HashSet<>());
        combination.setMusicians(new HashSet<>());  // // 这里要通过SingerDao来获取一个"select * from musician where idsinger = ?"的外键查询语句
        return combination;
    }
}
